<?php

namespace app\common\model\user;

use app\common\model\BaseModel;

/**
 * 用户等级模型
 */
class Grade extends BaseModel
{
    protected $pk = 'grade_id';
    protected $name = 'user_grade';

    /**
     * 用户等级模型初始化
     */
    public static function init()
    {
        parent::init();
    }

    /**
     * 获取详情
     */
    public static function detail($grade_id)
    {
        return (new static())->find($grade_id);
    }

    /**
     * 获取列表记录
     */
    public function getLists()
    {
        return $this->where('is_delete', '=', 0)
            ->field('grade_id,name')
            ->order(['weight' => 'asc', 'create_time' => 'asc'])
            ->select();
    }

    /**
     * 获取可用的会员等级列表
     */
    public static function getUsableList($appId = null)
    {
        $model = new static;
        $appId = $appId ? $appId : $model::$app_id;
        return $model->where('is_delete', '=', '0')
            ->where('app_id', '=', $appId)
            ->order(['weight' => 'asc', 'create_time' => 'asc'])
            ->select();
    }

    /**
     * 获取默认等级id
     */
    public static function getDefaultGradeId()
    {
        $grade = (new static())->where('is_default', '=', 1)->find();
        return $grade['grade_id'];
    }

    /**
     * 获取可购买的会员等级列表
     */
    public static function getBuyList($appId = null)
    {
        $model = new static;
        $appId = $appId ? $appId : $model::$app_id;
        return $model->where('is_delete', '=', '0')
            ->where('app_id', '=', $appId)
            ->where('price', '>', 0)
            ->order(['weight' => 'asc', 'create_time' => 'asc'])
            ->select();
    }

    /**
     * 获取可升级的会员等级列表
     */
    public static function getUpperList($user_id)
    {
        $user = User::detail($user_id);
        $model = new static;
        $appId = $user['app_id'];
        $weight = $user['grade']['weight'] ?? 0;
        return $model->where('is_delete', '=', '0')
            ->where('app_id', '=', $appId)
            ->where('weight', '>', $weight)
            ->order(['weight' => 'asc', 'create_time' => 'asc'])
            ->select();
    }

    public function getConditionAttr($value, $data)
    {
        $remark = '';
        if ($data['open_money'] == 1) {
            $money = sprintf('%.2f', $data['upgrade_money']);
            $remark .= "会员消费满{$money}元可升级到此等级";
        }
        if ($data['open_points'] == 1) {
            if (!empty($remark)) {
                $remark .= '\r\n';
            }
            $remark .= "会员积分满{$data['upgrade_points']}可升级到此等级";
        }
        if ($data['open_invite'] == 1) {
            if (!empty($remark)) {
                $remark .= '\r\n';
            }
            if($data['invite_grade']>0){
                $invite_grade = (new static())->where('grade_id',$data['invite_grade'])->value('name');
            }
            else{
                $invite_grade = "任意等级";
            }
            $remark .= sprintf("会员邀请%s人数满%d可升级到此等级",$invite_grade,$data['upgrade_invite']);
        }
        return $remark;
    }
}